package com.shey.qiuckpictest;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.shey.adapter.ChildPhotoAdapter;
import com.shey.base.BaseActivity;

import java.util.ArrayList;


public class ChildPhotoActivity extends BaseActivity {

    public static final String GROUP_NAME = "GroupName";
    public static final String CHILD_PHOTO_LIST = "ChildPhotoList";
    private static final String TAG = "ChildPhotoActivity";
    private GridView mGridView;
    private String mTitle;
    private ArrayList<String> childPhotoList = null;
    private ChildPhotoAdapter mChildPhotoAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState, View decorView) {
        enabledBackButton();

        mChildPhotoAdapter = new ChildPhotoAdapter(mContext, childPhotoList);
        mGridView.setAdapter(mChildPhotoAdapter);

        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putInt(ImagePagerActivity.IMAGE_INDEX, position);
                bundle.putStringArrayList(ImagePagerActivity.IMAGE_LIST, childPhotoList);

                forward(ImagePagerActivity.class,bundle,false);
            }
        });
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_childphoto;
    }

    @Override
    public void onPostContentView() {
        /**
         * 获取组名，以及组内所有图片的地址
         */
        Intent mIntent = getIntent();
        mTitle = mIntent.getStringExtra(GROUP_NAME);
        childPhotoList = mIntent.getStringArrayListExtra(CHILD_PHOTO_LIST);

        super.onPostContentView();

        if (childPhotoList.size() < 0 ) {
            toast(getString(R.string.content_was_losed));
            finish();
        }
    }

    @Override
    public void findView() {
        mToolBar.setTitle(mTitle + " (" + childPhotoList.size() + ")");
        mToolBar.setTitleTextColor(getApplicationContext().getResources().getColor(R.color.white));
        mGridView = (GridView) findViewById(R.id.childphoto_gridview);
    }

    @Override
    protected void onDestroy() {
        if (childPhotoList !=null && childPhotoList.size() > 0) {
            childPhotoList.clear();
            childPhotoList = null;
        }

        super.onDestroy();
    }
}
